<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="机构数量" prop="sMechanismnum">
      <el-input v-model="dataForm.sMechanismnum" placeholder="机构数量"></el-input>
    </el-form-item>
    <el-form-item label="商机数量" prop="sBussinessnum">
      <el-input v-model="dataForm.sBussinessnum" placeholder="商机数量"></el-input>
    </el-form-item>
    <el-form-item label="合同数量" prop="sContractnum">
      <el-input v-model="dataForm.sContractnum" placeholder="合同数量"></el-input>
    </el-form-item>
    <el-form-item label="日志数量" prop="sLognum">
      <el-input v-model="dataForm.sLognum" placeholder="日志数量"></el-input>
    </el-form-item>
    <el-form-item label="任务数量" prop="sTasknum">
      <el-input v-model="dataForm.sTasknum" placeholder="任务数量"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          sMonth: 0,
          sMechanismnum: '',
          sBussinessnum: '',
          sContractnum: '',
          sLognum: '',
          sTasknum: ''
        },
        dataRule: {
          sMechanismnum: [
            { required: true, message: '机构数量不能为空', trigger: 'blur' }
          ],
          sBussinessnum: [
            { required: true, message: '商机数量不能为空', trigger: 'blur' }
          ],
          sContractnum: [
            { required: true, message: '合同数量不能为空', trigger: 'blur' }
          ],
          sLognum: [
            { required: true, message: '日志数量不能为空', trigger: 'blur' }
          ],
          sTasknum: [
            { required: true, message: '任务数量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.sMonth = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.sMonth) {
            this.$http({
              url: this.$http.adornUrl(`/com/servicedata/info/${this.dataForm.sMonth}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sMechanismnum = data.servicedata.sMechanismnum
                this.dataForm.sBussinessnum = data.servicedata.sBussinessnum
                this.dataForm.sContractnum = data.servicedata.sContractnum
                this.dataForm.sLognum = data.servicedata.sLognum
                this.dataForm.sTasknum = data.servicedata.sTasknum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/com/servicedata/${!this.dataForm.sMonth ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'sMonth': this.dataForm.sMonth || undefined,
                'sMechanismnum': this.dataForm.sMechanismnum,
                'sBussinessnum': this.dataForm.sBussinessnum,
                'sContractnum': this.dataForm.sContractnum,
                'sLognum': this.dataForm.sLognum,
                'sTasknum': this.dataForm.sTasknum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
